import java.util.Arrays;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-06-03
 * Time: 7:24
 */
public class Tmp {
    private static void merge(int[] array,int low,int mid,int high) {

        int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = high;

        int[] tmpArr = new int[high-low+1];
        int k = 0;//代表tmpArr的下标
        //证明两个段都有数据
        while (s1 <= e1 && s2 <= e2) {
            if(array[s1] <= array[s2]) {
                tmpArr[k++] = array[s1++];
            }else {
                tmpArr[k++] = array[s2++];
            }
        }
        //剩余元素，全部挪过来
        while (s1 <= e1) {
            tmpArr[k++] = array[s1++];
        }
        //剩余元素，全部挪过来
        while (s2 <= e2) {
            tmpArr[k++] = array[s2++];
        }
        for (int i = 0; i < tmpArr.length; i++) {
            array[i+low] = tmpArr[i];
        }
    }
    public static void mergeSortNor(int[] arr){
        int gap=1;
        while(gap<arr.length){
            for(int i=0;i<arr.length;i+=gap*2){
                int left=i;
                int mid=left+gap-1;
                if(mid>=arr.length){
                    mid=arr.length-1;
                }
                int right=mid+gap;
                if(right>=arr.length){
                    right=arr.length-1;
                }
                merge(arr,left,mid,right);

            }
            gap*=2;
        }
    }

    public static void main(String[] args) {
        int[] arr={9,1,2,5,7,4,8,6,3,5};
        mergeSortNor(arr);
        System.out.println(Arrays.toString(arr));
    }
}
